<template>
  <el-dialog
    v-loading="loading"
    class="assign-role"
    title="分配角色"
    :visible="showRoleDialog"
    @close="closeDialog"
    @open="handleOpenDialog"
  >
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="checkList">
      <!-- 特殊 收集提交给后台的是label，文字描述=>默认插槽  写在标签内部-->
      <el-checkbox
        v-for="item in roleList"
        :key="item.id"
        :label="item.id"
      >{{ item.name }}</el-checkbox>

    </el-checkbox-group>

    <template #footer>
      <el-button type="primary" size="small" @click="clickSubmit">确定</el-button>
      <el-button size="small" @click="closeDialog">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { reqGetRoleList } from '@/api/setting'
import { reqGetBaseInfo } from '@/api/user'
import { reqAssignRoles } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      checkList: [],
      roleList: [],
      loading: false
    }
  },
  methods: {
    closeDialog() {
      this.$emit('update:showRoleDialog', false)
      // 关闭对话框清空上一个员工的回显信息
      this.checkList = []
    },

    handleOpenDialog() {
      this.loading = true
      //   this.getRoleList()
      // 获取员工详情 回显
      //   this.getEmployeeDetail()

      Promise.all([this.getRoleList(), this.getEmployeeDetail()]).then(() => {
        this.loading = false
      })
    },

    async  getRoleList() {
      const { data: { rows }} = await reqGetRoleList(1, 99999)
      console.log(rows, 33333)
      this.roleList = rows
    },

    async getEmployeeDetail() {
    //   const { data } = await reqGetBaseInfo(this.userId)
    //   console.log(data, 22222222)
      //   this.checkList = roleIds

      const { data: { roleIds }} = await reqGetBaseInfo(this.userId)
      console.log(roleIds, 22222222)
      this.checkList = roleIds
    },

    async clickSubmit() {
      console.log(1111)
      const res = await reqAssignRoles(this.userId, this.checkList)
      console.log(res, 555555)
      this.$message.success('分配角色成功')
      this.closeDialog()
    }
  }

}
</script>

<style lang="scss" scoped>
.assign-role {
  ::v-deep {
    .el-checkbox {
      font-size: 30px;
    }
  }
}
</style>
